package org.wutopia.labcloud.domain.file.controller;


import io.minio.MinioClient;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.wutopia.labcloud.library.common.base.CodedMessage;
import org.wutopia.labcloud.library.common.base.CommonResponse;
import org.wutopia.labcloud.domain.file.controller.vo.UploadUrlInputVO;
import org.wutopia.labcloud.service.api.service.dubbo.FileService;
import org.wutopia.labcloud.service.api.service.dubbo.dto.input.UploadUrlInputDTO;

@RestController
@AllArgsConstructor
@RequestMapping("/file")
@Slf4j
public class MinioFileController {

    private MinioClient minioClient;

    private FileService fileService;


    @PostMapping("/upload_url")
    public ResponseEntity<CodedMessage<String>> uploadUrl(@RequestBody UploadUrlInputVO vo)  {

        if( vo==null || vo.getObjectName() == null || vo.getBucket() == null) {
            return CommonResponse.failure("入参错误");
        }
        log.info("upload_url input bucket: {}, objectName: {}", vo.getBucket(),vo.getObjectName());
        // 生成上传链接（有效期 1 小时）
        UploadUrlInputDTO dto = new UploadUrlInputDTO();
        dto.setBucket(vo.getBucket());
        dto.setObjectName(vo.getObjectName());

        String uploadUrl = fileService.getUploadUrl(dto);

        return CommonResponse.successWithData(uploadUrl);
    }
}
